﻿using Microsoft.EntityFrameworkCore;
using ProjectCore.Domain.DomainService;
using ProjectCore.Domain.Model.Entity;
using ProjectCore.Domain.Repository.Interfaces;
using ProjectCore.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProjectCore.Infrastructure.DomainService
{
    public class ProductDomainService: IProductDomainService
    {
        private readonly IBaseRepository<Product> baseRepository;
        private readonly IBaseRepository<SkuCar> skucarbase;
        private readonly IBaseRepository<SkuPhone> skuphonebase;
        private readonly IBaseRepository<Setting> setbase;
        private readonly MyContext myContext;
        public ProductDomainService(IBaseRepository<Product> _baseRepository, MyContext _myContext, IBaseRepository<SkuCar> _skucarbase, IBaseRepository<SkuPhone> _skuphonebase, IBaseRepository<Setting> _setbase)
        {
            baseRepository = _baseRepository;
            myContext = _myContext;
            skucarbase = _skucarbase;
            skuphonebase = _skuphonebase;
            setbase = _setbase;
        }


        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="product"></param>
        /// <returns></returns>
        public async Task<int> Add(Product product)
        {
            return await baseRepository.AddEntityAsync(product);
            
        }


        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<Product> GetProduct(int Id)
        {
            return await myContext.Products.FindAsync(Id);
        }


        /// <summary>
        /// 逻辑删除
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<int> IsDel(int Id)
        {
            Eval eval = myContext.Evals.Where(x => x.EvalID.Equals(Id)).FirstOrDefault();
            eval.IsDeleted = true;
            return await myContext.SaveChangesAsync();
            //    string sql = $"update Products set IsDeleted = 1 where ProID = {Id}";
            //    return myContext.Database.ExecuteSqlCommand(sql);
        }


        /// <summary>
        /// 其他设置添加
        /// </summary>
        /// <param name="setting"></param>
        /// <returns></returns>
        public async  Task<int> SettingAdd(Setting setting)
        {
            return await setbase.AddEntityAsync(setting);
        }


        /// <summary>
        /// 其他设置反填
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<Setting> GetSetting(int Id)
        {
            return await myContext.Settings.FindAsync(Id);
        }


        /// <summary>
        /// 其他设置修改
        /// </summary>
        /// <param name="setting"></param>
        /// <returns></returns>
        public async Task<int> SettingUpt(Setting setting)
        {
            return await setbase.UpdateEntityAsync(setting);
        }


        /// <summary>
        /// 汽车Sku添加
        /// </summary>
        /// <param name="car"></param>
        /// <returns></returns>
        public async Task<int> SkuCarAdd(SkuCar car)
        {
            return await skucarbase.AddEntityAsync(car);
        }


        /// <summary>
        /// 汽车Sku反填
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<SkuCar> GetCar(int Id)
        {
            return await myContext.SkuCars.FindAsync(Id);
        }


        /// <summary>
        /// 汽车Sku修改
        /// </summary>
        /// <param name="car"></param>
        /// <returns></returns>
        public async Task<int> SkuCarUpt(SkuCar car)
        {
            return await skucarbase.UpdateEntityAsync(car);
        }


        /// <summary>
        /// 手机Sku添加
        /// </summary>
        /// <param name="phone"></param>
        /// <returns></returns>
        public async Task<int> SkuPhoneAdd(SkuPhone phone)
        {
            return await skuphonebase.AddEntityAsync(phone);
        }


        /// <summary>
        /// 手机Sku反填
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<SkuPhone> GetPhone(int Id)
        {
            return await myContext.SkuPhones.FindAsync(Id);
        }


        /// <summary>
        /// 手机Sku修改
        /// </summary>
        /// <param name="phone"></param>
        /// <returns></returns>
        public async Task<int> SkuPhoneUpt(SkuPhone phone)
        {
            return await skuphonebase.UpdateEntityAsync(phone);
        }


        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="product"></param>
        /// <returns></returns>
        public async Task<int> Upt(Product product)
        {
            return await baseRepository.UpdateEntityAsync(product);
        }

    }
}
